#include "adc_battery.h"

static volatile u16 Adc_Battery[10];


/**
 * @Description ADC初始化，读取电池电压
 */
void ADC_Battery_Init()
{	
	DMA_InitTypeDef DMA_InitStructure;
	GPIO_InitTypeDef  GPIO_InitStructure;
	ADC_CommonInitTypeDef ADC_CommonInitStructure;
	ADC_InitTypeDef       ADC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_DMA2, ENABLE);	//使能GPIOB、DMA2时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 						//使能ADC1时钟

	//配置IO口
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;									//PB0 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;								//GPIO输入模式
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;							//不需要上拉
	GPIO_Init(GPIOB, &GPIO_InitStructure);										//配置GPIOB0  
 
	//使能ADC时钟
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);	  						
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);		 
 
	//配置ADC
	ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;						//ADC转换独立模式
	ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;	//ADC采样间隔20个周期
	ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;			//关闭DMA内存访问（多ADC模式下）
	ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;						//设置ADC分频系数为4
	ADC_CommonInit(&ADC_CommonInitStructure);										//配置ADC外设
	
	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;							//设置ADC转换精度为12位
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;									//关闭扫描模式	
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;								//使能连续转换
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;		//关闭外部触发ADC
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;							//ADC转换数据右对齐	
	ADC_InitStructure.ADC_NbrOfConversion = 1;										//规则转换中只有一个通道  
	ADC_Init(ADC1, &ADC_InitStructure);
	
	ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_480Cycles );	//设置规则转换通道ADC1通道8、采样时间为480个周期
	ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); 								//在一次转换后发起DMA请求
	ADC_DMACmd(ADC1, ENABLE);           											//使能ADC1外设及DMA
	ADC_Cmd(ADC1, ENABLE);

	//设置ADC的DMA2_Stream0 通道0 
	DMA_DeInit(DMA2_Stream0); 
	DMA_InitStructure.DMA_BufferSize = 5;											//DMA缓存单元数量
	DMA_InitStructure.DMA_Channel = DMA_Channel_0;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;							//ADC外设数据到内存
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;							//关闭FIFO模式
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Adc_Battery;   				//内存DMA的起始地址
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;						//DMA突发模式、一个节拍传输半字
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;				//DMA传输数据单元为半字
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;							//内存地址自动增长
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;									//循环模式
	DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_BASE+0x4C;  					//ADC_DR寄存器地址
	DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;				//外设地址不变
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;								//DMA优先级高
	DMA_Init(DMA2_Stream0,&DMA_InitStructure);
	DMA_Cmd(DMA2_Stream0,ENABLE);	
	
	ADC_SoftwareStartConv(ADC1); //开启ADC转换
}


/**
 * @Description 获取电压
 */
float ADC_Get_battery()
{
	//4096级分辨率、参考电压3.3v
	float RTbattery = 4.43f*3.3f*((float)Adc_Battery[0]/4096);
	return RTbattery;
}


/**
 * @Description 将新的电压数据输入数组，返回电压平均值
 */
float Average_battery(float batteryValue)
{
	static int measure=8;
	int i;
	float Average_Value = 0;
	//电压值数组
	static float BatteryValue[10] = {0.0};
	//输入新数据
	BatteryValue[measure-1] = batteryValue;
	//数组元素移动
	for(i=0;i<measure-1;i++)
	{
		BatteryValue[i]=BatteryValue[i+1];
	}
	//计算平均值
	for(i=0;i<measure;i++)
	{
		Average_Value += BatteryValue[i];
	}
	Average_Value /= (measure);
	return Average_Value;
}
